package dk.viauc.cmclang.intlang;

import java.util.Arrays;


public class Token {
	public byte kind;
	public String spelling;
	public String type;

	public  Token(String spell)
	{
		for (int i=0;i<SPELLINGS.length;i++)
		{
			if(SPELLINGS[i].equals(spell))
			{
				kind=(byte)i;
				break;
			}
		}
		spelling=spell;
		if(isType())
			kind=Token.TYPE;
		if(isOperator())
			kind=Token.OPERATOR;
		if(spell.equals("MAIN"))
			kind=Token.IDENTIFIER;
		if(spell.equals("TRUE")||spell.equals("FALSE"))
			kind=Token.BOOLITERAL;
		
	}
	
	public Token(byte kind, String spelling) {
		this.kind = kind;
		this.spelling = spelling;

		if (kind == IDENTIFIER) {
			if (isOperator()) {
				this.kind = Token.OPERATOR;
			}
			if (isType()) {
				this.kind = Token.TYPE;
			}	

			for (byte i = 0; i < SPELLINGS.length; ++i)
				if (spelling.equals(SPELLINGS[i])) {
					this.kind = i;
					break;
				}
		

		}
	}

	private boolean isOperator() {
		for(int i = 0; i <OPERAT.length; i ++) {
			if(spelling.equals(OPERAT[i]))
				return true;
		}
		return false;
	}
	
	private boolean isType() {
		for (int i = 0; i < TYPES.length; i++) {
			if (spelling.equals(TYPES[i]))
				return true;
		}
		return false;
	}
	
	public boolean isAssignOperator()
	{
		if( kind == OPERATOR )
			return containsOperator( spelling, ASSIGNOPS );
		else
			return false;
	}
	
	public boolean isMulOperator()
	{
		if( kind == OPERATOR )
			return containsOperator( spelling, MULOPS );
		else
			return false;
	}
	
	public boolean isBoolOperator()
	{
		if( kind == OPERATOR )
			return containsOperator( spelling, BOOLOPS );
		else
			return false;
	}
	
	
	public boolean isAddOperator()
	{
		if( kind == OPERATOR )
			return containsOperator( spelling, ADDOPS );
		else
			return false;
	}

	private boolean containsOperator( String spelling, String OPS[] )
	{
		for( int i = 0; i < OPS.length; ++i )
			if( spelling.equals( OPS[i] ) )
				return true;
				
		return false;
	}

	public static final byte  ARRAYOF = 0;
	public static final byte  OPERATOR = 1;
	public static final byte  AT = 2;
	public static final byte  BEGIN = 3;
	public static final byte  DO = 4;
	public static final byte  ELSE = 5;
	public static final byte  ENDFUNCTION = 6;
	public static final byte  ENDIF = 7;
	public static final byte  ENDWHILE = 8;
	public static final byte  EOL = 9;
	public static final byte  FUNCTION = 10;
	public static final byte  IF = 11;
	public static final byte  KINDOF = 12;
	public static final byte  LET = 13;
	public static final byte  READ = 14;
	public static final byte  SIZEOF = 15;
	public static final byte  WHILE = 16;
	public static final byte  WITHPARAMS = 17;
	public static final byte  WRITE = 18;
	public static final byte  OR = 19;
	public static final byte  AND = 20;
	public static final byte  NOT = 21;
	public static final byte  ERROR = 22;
	public static final byte  IDENTIFIER = 23;
	public static final byte  INTEGERLITERAL = 24;
	public static final byte  EOT = 25;
	public static final byte  CALL = 26;
	public static final byte  TYPE = 27;
	public static final byte  BOOLITERAL=28;
	public static final byte  ELIF=29;
	public static final byte RETURN = 30;

	private static final String SPELLINGS[] = { 
		"ARRAYOF", 
		"OPERATOR", 
		"AT", 
		"BEGIN", 
		"DO",
		"ELSE", 
		"ENDFUNCTION", 
		"ENDIF", 
		"ENDWHILE", 
		"EOL", 
		"FUNCTION",
		"IF", 
		"KINDOF", 
		"LET", 
		"READ", 
		"SIZEOF", 
		"WHILE",
		"WITHPARAMS", 
		"WRITE", 
		"OR", 
		"AND",
		"NOT", 
		"ERROR", 
		"IDENTIFIER", 
		"INTEGERLITERAL",
		"EOT", 
		"CALL", 
		"TYPE",
		"BOOLITERAL",
		"ELIF",
		"RETURN"

	};
	
	public static final String[] OPERAT = {
		"BE", "PLUS", "MINUS", "MUL", "DIV", "MOD", "LESS", "MORE", "EQUAL", "LESSEQ", "MOREEQ", "OR", "AND",
		"NOT"
	};
	
	public static final String[] BOOLOPS = {   "LESS", "MORE", "EQUAL", "LESSEQ", "MOREEQ", "OR", "AND",
			"NOT"
	};
	public static final String[] TYPES = { "INT", "BOOL"

	};
	public static final String[] BOOLLITS ={
		"TRUE","FALSE"
		
	};
	private static final String[] ASSIGNOPS =
	{
		"BE"
	};
		
	private static final String[] ADDOPS =
	{
		"PLUS", "MINUS"
	};
		
	private static final String[] MULOPS =
	{
		"MUL", "DIV", "MOD"
	};

}